const {
  getOrderList,
  cancelOrder
} = require("../../../api/api.js");
const pageMixin = require('../../../mixin/pageMixin.js');
const {
  _showToast
} = require('../../../utils/util.js')
const {
  _navigateTo
} = require('../../../utils/util.js');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  mixins: [pageMixin],
  data: {
    active: 0,
    orderTitle: {
      0: '全部',
      1: '待付款',
      2: '已付款'
    },
    loadMethod: getOrderList,
    imgHost: '',
    loadOptions: {
      currentType: 0,
      typeKey: 'status'
    },
    allType: ['ALL', 0, 1]
  },
  /**
   * 页面的初始数据
   */

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const activeIndex = options.status;
    this.setData({
      imgHost: app.img_host,
      active: activeIndex,
      ['loadOptions.currentType']: this.data.allType[activeIndex]
    }, function () {
      this.loadData();
    })
  },
  change(e) {
    this.changeTabHandler({
      detail: e.detail.index
    })
  },
  cancel(e) {
    //当前的订单状态
    const orderNumber = e.currentTarget.dataset.number;
    const index = e.currentTarget.dataset.index
    if (orderNumber) {
      cancelOrder({
        order_number: orderNumber
      }).then(res => {
        _showToast(res.msg);
        this.setData({
          [`listData[${index}].order_status`]: -1
        })
      }).catch((err) => {
        console.log(err)
      })
    }
  },
  onReachBottom() {
    this.bottomLoading().catch((e) => {
      console.log(e)
    })
  },
  //跳转到订单详情页
  toOrderDetail(e) {
    const {
      id
    } = e.currentTarget.dataset;
    console.log("订单页面的id")
    console.log(id)
    app.id = id
    _navigateTo(`./order-detail/index?id=${id}`);
  }
})